(function () {
	new Vue({
		el: '#jwk-matching',
		data() {
			return {
				unitList: [],
				isNewAdd: false, //新增编辑栏
				newValue: '',
				module_code: 'CSTM_JWK',
				config_name: 'CSTM_JWK_PARK_FACILITY_TYPE',
			}
		},
		mounted() {
			this.getMatingData();
		},
		methods: {
			//获取配套数据
			getMatingData() {
				var that = this
				hlhttp.get({
					url: 'configure/dict',
					data: {
						module_code: this.module_code,
						config_name: this.config_name
					},
					success: function (data) {
						data.map(item => {
							item.isEdit = false
						})
						that.unitList = data
					},
				})
			},
			// 确认编辑
			sureEdit(item) {
				var that = this;
				hlhttp.put({
					url: 'configure/user_defined_dict',
					data: {
						"module_code": this.module_code,
						"config_name": this.config_name,
						"dict_code": item.dictCode,
						"value": item.dictName
					},
					success: function (res) {
						item.isEdit = false
					}
				})
			},
			// 启用或者停用
			switchFunc(item) {
				var that = this;
				hlhttp.put({
					url: 'configure/user_defined_dict/enable_and_disable',
					data: {
						"module_code": this.module_code,
						"config_name": this.config_name,
						"dict_code": item.dictCode,
						"status": item.status == 'Y' ? 'N' : 'Y'
					},
					success: function (res) {
						item.isEdit = false
						that.getMatingData()
					}
				})
			},
			// 删除
			deleteItem(item) {
				var that = this;
				hlhttp.delete({
					url: 'configure/user_defined_dict?module_code=' + this.module_code + '&config_name=' + this.config_name + '&dict_code=' + item.dictCode,
					data: {
						"module_code": this.module_code,
						"config_name": this.config_name,
						"dict_code": item.dictCode,
					},
					success: function (res) {
						hl.toast('删除成功');
						that.getMatingData()
					}
				})
			},
			newAddBut() {
				for (let n = 0; n < this.unitList.length; n++) {
					if (this.unitList[n].isEdit) {
						hl.toast('请先保存')
						return
					}
				}
				this.isNewAdd = true
				this.newValue = ''
			},
			// 新增
			newAdd() {
				if (!this.newValue) {
					hl.toast('请输入名称')
					return
				}
				var that = this;
				hlhttp.post({
					url: 'configure/user_defined_dict',
					data: {
						"module_code": this.module_code,
						"config_name": this.config_name,
						"parent_code": "#",
						"value": this.newValue
					},
					success: function (res) {
						hl.toast('添加成功');
						that.isNewAdd = false
						that.getMatingData()
					}
				})
			}
		},
	})
}())
